﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DFYE.Web.Areas.manage.Controllers
{
    using DFYE.Web.Models;
    using JULONG.WEB.Library;
    public class studentDailyTypeController : Controller
    {
        //
        // GET: /manage/studentDailyTyp/
        DataSession<entityDBContainer, studentDailyType> db = new DataSession<entityDBContainer, studentDailyType>();
        public ActionResult Index()
        {
            
            return View(db.query());
        }


        //
        // GET: /manage/studentDailyTyp/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /manage/studentDailyTyp/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            studentDailyType stType = new studentDailyType();
            TryUpdateModel(stType);

            try
            {
                db.insert(stType);

                return RedirectToAction("Index");
            }
            catch
            {
                return View(stType);
            }
        }
        
        //
        // GET: /manage/studentDailyTyp/Edit/5
 
        public ActionResult Edit(int id)
        {
            studentDailyType stType = db.get(d => d.ID == id);

            return View(stType);
        }

        //
        // POST: /manage/studentDailyTyp/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            studentDailyType stType = db.get(d => d.ID == id);
            TryUpdateModel(stType);
            try
            {
                db.save();

                return RedirectToAction("Index", new { staute = true });

            }
            catch
            {
                return View(stType);
            }
        }
        
        //
        // POST: /manage/studentDailyTyp/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var entity = db.get(d => d.ID==id);

            if (entity.requirement) {

                ModelState.AddModelError("info", "该记录系统标记为不可删除");
                return View(entity);
            }

            try
            {
                db.delete(d => d.ID == id);


                // TODO: Add delete logic here

                return RedirectToAction("Index", new { staute = true });
            }
            catch
            {
                return View(entity);
            }
        }
    }
}
